package gui.visu.dialogs;

import gui.visu.listeners.ColorWheelListener;

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JPanel;

import controls.DataControl;

import tuio.components.LabelTouch;

import data.Constants;
import data.visu.utils.ImageLoader;

/**
 * @author Charles Perin
 * 29 mars 2011
 * LIMSI - INRIA Rennes
 * charles.perin@free.fr
 *
 */
public class JDialogColorWheel extends JDialog {


	private static final long serialVersionUID = 1522851692474470104L;

	private Color selectedColor = Constants.DISPLAY_DATA_DEFAULT_COLOR;

	private LabelTouch imageLabel;

	private ColorWheelListener listener;
	
	private ImageIcon imageWheel, imageWheelPressed;
	
	private DataControl dataControl;
	
	private int mode;
	
	private JDialog dialogParent;
	
	public static final int DISPLAY_DATA=0,MOUSE=1,SELECTION=2,CREATE_DISPLAY_DATA=3;;
	
	public JDialogColorWheel(DataControl dataControl, String title, int mode){
		super(dataControl.getIhm(), title, false);
		this.dataControl = dataControl;
		this.mode=mode;
		setLocationRelativeTo(null);
		setUndecorated(true);
		setResizable(false);
		
		imageWheel = ImageLoader.createImageIcon("colorWheel.png");
		imageWheelPressed = ImageLoader.createImageIcon("colorWheelOk.png");
		initComponent();
		pack();
	}
	
	public JDialogColorWheel(DataControl dataControl, String title,	int mode, JDialogAddDisplayDataAtPosition dialogParent) {
		super(dataControl.getIhm(), title, false);
		this.dataControl = dataControl;
		this.mode=mode;
		this.dialogParent = dialogParent;
		setLocationRelativeTo(null);
		setUndecorated(true);
		setResizable(false);
		
		imageWheel = ImageLoader.createImageIcon("colorWheel.png");
		imageWheelPressed = ImageLoader.createImageIcon("colorWheelOk.png");
		initComponent();
		pack();
	}

	public void initComponent(){
		this.getContentPane().setLayout(new BorderLayout());
		
		JPanel content = new JPanel();
		
		imageLabel = new LabelTouch(imageWheel);
		content.add(imageLabel);
		listener = new ColorWheelListener(this, dialogParent);
		imageLabel.addMouseListener(listener);

		imageLabel.setOpaque(true);
		imageLabel.setBackground(Constants.DISPLAY_DATA_DEFAULT_COLOR);
		
		this.getContentPane().add(content, BorderLayout.CENTER);
	}


	public void init() {
		listener.setButtonOkDimension(imageLabel.getSize());
	}
	
	
	public Color getSelectedColor() {
		return selectedColor;
	}

	public void setSelectedColor(Color selectedColor) {
		this.selectedColor = selectedColor;
		imageLabel.setBackground(selectedColor);
	}

	public void okPressed(boolean pressed) {
		if(pressed)imageLabel.setIcon(imageWheelPressed);
		else imageLabel.setIcon(imageWheel);
		repaint();
	}

	public void modifyColor() {
		
		switch(mode){
		case DISPLAY_DATA:
			dataControl.setSelectedDisplayDataColor(
					new Color(selectedColor.getRed(),
					selectedColor.getGreen(),
					selectedColor.getBlue(),
					Constants.DISPLAY_DATA_DEFAULT_TRANSPARENCY));
			break;
		case MOUSE:
			dataControl.setSelectedMouseColor(
					new Color(selectedColor.getRed(),
					selectedColor.getGreen(),
					selectedColor.getBlue(),
					Constants.DISPLAY_DATA_DEFAULT_TRANSPARENCY));
			break;
		case SELECTION:
			dataControl.setSelectedSelectionColor(
					new Color(selectedColor.getRed(),
					selectedColor.getGreen(),
					selectedColor.getBlue(),
					Constants.DISPLAY_DATA_DEFAULT_TRANSPARENCY));
			break;
		case CREATE_DISPLAY_DATA:
			if(dialogParent!=null){
				System.out.println("dialogParent non null !");
				((JDialogAddDisplayDataAtPosition)dialogParent).setColorDisplayData(new Color(selectedColor.getRed(),
						selectedColor.getGreen(),
						selectedColor.getBlue(),
						Constants.DISPLAY_DATA_DEFAULT_TRANSPARENCY));
				if(((JDialogAddDisplayDataAtPosition)dialogParent).getColorDisplayData()==null)((JDialogAddDisplayDataAtPosition)dialogParent).setColorDisplayData(Constants.DISPLAY_DATA_DEFAULT_COLOR);
			}
			break;
		}
		
		
	}
}
